package com.caigp.cookbook2.fragment;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.caigp.cookbook2.R;
import com.caigp.cookbook2.activity.DetailActivity;
import com.caigp.cookbook2.adapter.ListAdapter;
import com.caigp.cookbook2.bean.CookBook;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ListFragment extends Fragment {

    private View view;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<CookBook> cookBookList = new ArrayList<>();

    private ListAdapter listAdapter;

    private RecyclerViewListener recyclerViewListener;

    float downY;

    float downX;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_list, container, false);
        ButterKnife.bind(this, view);
        initRecyclerView();
        return view;
    }

    private void initRecyclerView() {
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        listAdapter = new ListAdapter(R.layout.item_list, cookBookList);
        listAdapter.bindToRecyclerView(recyclerView);
        listAdapter.setEmptyView(getLayoutInflater().inflate(R.layout.item_empty, null));
        listAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerViewListener.scrollToBottom();
            }
        }, recyclerView);
        listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getContext(), DetailActivity.class);
                intent.putExtra("cookBook", cookBookList.get(position));
                startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(listAdapter);
        recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

                switch (e.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downY = e.getY();
                        downX = e.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        float upY = e.getY();
                        float upX = e.getX();

                        if (upY > downY ? upY - downY < 100: downY - upY < 100 && upX > downX ? upX - downX > 300: downX - upX > 300) {
                            getActivity().finish();
                            return true;
                        }
                        break;
                }

                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
    }

    public void setCookBookList(List<CookBook> cookBookList) {
        this.cookBookList.addAll(cookBookList);
        listAdapter.loadMoreComplete();
        listAdapter.notifyDataSetChanged();
    }

    /**
     * 清空全部数据
     */
    public void clearCookBookList() {
        this.cookBookList.clear();
        listAdapter.notifyDataSetChanged();
    }

    /**
     * 数据全部加载
     */
    public void setLoadEnd() {
        listAdapter.loadMoreEnd();
    }

    /**
     * 数据加载错误
     */
    public void setLoadMoreFail() {
        listAdapter.loadMoreFail();
    }

    public void setRecyclerViewListener(RecyclerViewListener recyclerViewListener) {
        this.recyclerViewListener = recyclerViewListener;
    }

    public interface RecyclerViewListener {

        void scrollToTop();

        void scrollToBottom();
    }
}
